﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLPivotViewer
{
    public partial class App : Application
    {

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }
        public string CXMLUrl { get; private set; }
        public string Facets { get; private set; }
        public string Infos { get; private set; }
        public string ImageField { get; private set; }
        public string ImageSubstitue { get; private set; }
        public string FieldUrl { get; private set; }
        public string ItemOpen { get; private set; }


        public string ARGBFilter { get; private set; }
        public string ARGBInfo { get; private set; }
        public string ARGBContent { get; private set; }
        public string ARGBTitle { get; private set; }


        public string StretchFilter { get; private set; }
        public string StretchInfo { get; private set; }
        public string StretchContent { get; private set; }
        public string StretchTitle { get; private set; }

        public string URLFilter { get; private set; }
        public string URLTitle { get; private set; }
        public string URLContent { get; private set; }
        public string URLInfo { get; private set; }

        public Visibility VisibilityFilter { get; private set; }
        public Visibility VisibilityTitle { get; private set; }
        public Visibility VisibilityInfo { get; private set; }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();
            string cxml;
            string Facets;
            string Infos;
            string ImageField;
            string ImageSubstitue;
            string FieldUrl;
            string itemopen;


            string ARGBFilter;
            string ARGBInfo;
            string ARGBContent;
            string ARGBTitle;

            string URLFilter;
            string URLTitle;
            string URLContent;
            string URLInfo;


            string StretchFilter;
            string StretchInfo;
            string StretchContent;
            string StretchTitle;


            string HideFilter;
            string HideInfo;
            string HideTitle;

            if (e.InitParams.TryGetValue("cxml", out cxml))
            {
                this.CXMLUrl = cxml;
            }
            if (e.InitParams.TryGetValue("itemOpen", out itemopen))
            {
                this.ItemOpen = itemopen;
            }
            if (e.InitParams.TryGetValue("Facets", out Facets))
            {
                this.Facets = Facets;
            }
            if (e.InitParams.TryGetValue("Infos", out Infos))
            {
                this.Infos = Infos;
            }
            if (e.InitParams.TryGetValue("ImageField", out ImageField))
            {
                this.ImageField = ImageField;
            }
            if (e.InitParams.TryGetValue("ImageSubstitue", out ImageSubstitue))
            {
                this.ImageSubstitue = ImageSubstitue;
            }
            if (e.InitParams.TryGetValue("FieldUrl", out FieldUrl))
            {
                this.FieldUrl = FieldUrl;
            }

            if (e.InitParams.TryGetValue("ARGBContent", out ARGBContent))
            {
                this.ARGBContent = ARGBContent;
            }

            if (e.InitParams.TryGetValue("ARGBFilter", out ARGBFilter))
            {
                this.ARGBFilter = ARGBFilter;
            }

            if (e.InitParams.TryGetValue("ARGBTitle", out ARGBTitle))
            {
                this.ARGBTitle = ARGBTitle;
            }

            if (e.InitParams.TryGetValue("ARGBInfo", out ARGBInfo))
            {
                this.ARGBInfo = ARGBInfo;
            }


            if (e.InitParams.TryGetValue("URLContent", out URLContent))
            {
                this.URLContent = URLContent;
            }

            if (e.InitParams.TryGetValue("URLFilter", out URLFilter))
            {
                this.URLFilter = URLFilter;
            }

            if (e.InitParams.TryGetValue("URLTitle", out URLTitle))
            {
                this.URLTitle = URLTitle;
            }

            if (e.InitParams.TryGetValue("URLInfo", out URLInfo))
            {
                this.URLInfo = URLInfo;
            }

            if (e.InitParams.TryGetValue("StretchFilter", out StretchFilter))
            {
                this.StretchFilter = StretchFilter;
            }
            if (e.InitParams.TryGetValue("StretchTitle", out StretchTitle))
            {
                this.StretchTitle = StretchTitle;
            }
            if (e.InitParams.TryGetValue("StretchContent", out StretchContent))
            {
                this.StretchContent = StretchContent;
            }
            if (e.InitParams.TryGetValue("StretchInfo", out StretchInfo))
            {
                this.StretchInfo = StretchInfo;
            }


            if (e.InitParams.TryGetValue("HideTitle", out HideTitle))
            {
                if (HideTitle.ToLower() == "true")
                    this.VisibilityTitle = Visibility.Collapsed;
                else
                    this.VisibilityTitle = Visibility.Visible;
            }
            if (e.InitParams.TryGetValue("HideInfo", out HideInfo))
            {
                if (HideInfo.ToLower() == "true")
                    this.VisibilityInfo = Visibility.Collapsed;
                else
                    this.VisibilityInfo = Visibility.Visible;
            }
            if (e.InitParams.TryGetValue("HideFilter", out HideFilter))
            {
                if (HideFilter.ToLower() == "true")
                    this.VisibilityFilter = Visibility.Collapsed;
                else
                    this.VisibilityFilter = Visibility.Visible;
            }


        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }
    }
}
